package com.andova.net;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.andova.component.net.ResponseModel;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;

import retrofit2.Call;


/**
 * Created by Administrator on 2019-02-12.
 *
 * @author kzaxil
 * @since 0.0.4
 * @deprecated
 */
public abstract class NetworkHandleManager2 {
    private WeakReference<View> mRefView;
    private Map<String, NetCallback2> mNetMap = new WeakHashMap<>(3);

    protected <T extends ResponseModel> NetCallback2<T> netCallback2(final String key, View view, boolean reset,
                                                                     NetworkHandleFunc2<T> func,
                                                                     Class<T> modelClass) {
        return netCallback2(true, false, key, view, reset, func, modelClass);
    }

    protected <T extends ResponseModel> NetCallback2<T> netCallback2(final boolean editLog,
                                                                     final boolean toast,
                                                                     final String key,
                                                                     @Nullable View view,
                                                                     boolean reset,
                                                                     final NetworkHandleFunc2<T> func,
                                                                     @Nullable Class<T> modelClass) {
        if (view != null) {
            if (mRefView != null) mRefView.clear();
            mRefView = new WeakReference<>(view);
        } else {
            mRefView = null;
        }
        NetCallback2<T> callback = mNetMap.get(key);
        if (callback == null) {
            callback = callback(editLog, toast, mRefView, func, modelClass);
            if (callback == null) {
                callback = new NetCallback2<T>() {
                    @NonNull
                    @Override
                    protected NetworkHandleFunc2<T> func() {
                        return func;
                    }
                };
            }
            mNetMap.put(key, callback);
        }
        if (reset) callback.reset();
        return callback;
    }

    @Nullable
    protected abstract <T extends ResponseModel> NetCallback2<T> callback(boolean editLog, boolean toast,
                                                                          @Nullable WeakReference<View> refView,
                                                                          NetworkHandleFunc2<T> func,
                                                                          Class<T> modelClass);

    public <T extends ResponseModel> void request(@NonNull Context context, @NonNull Call<T> call, NetworkHandleFunc2<T> func) {
        NetCallback2<T> callback = netCallback2(true, true, "default", null, true, func, null);
        callback.call(new WeakReference<>(context), call);
    }

    public <T extends ResponseModel> void request(@NonNull Context context, @NonNull Call<T> call, NetworkHandleFunc2<T> func,
                                                  boolean editLog, boolean toast, String tag, View view, boolean reset, Class<T> modelClass) {
        NetCallback2<T> callback = netCallback2(editLog, toast, tag, view, reset, func, modelClass);
        callback.call(new WeakReference<>(context), call);
    }
}
